<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <!-- <div style="width: 100px; height: 100px; background: #123450;"></div> -->
    <p style="color: red;"></p>
  

    <script>
        // 封装一个方法、输出刚刚、几秒前、几小时等
        function tm(m){
            var time1=new Date();
            var time2=new Date(m);
            var digit=parseInt((time1.getTime()-time2.getTime())/1000);
            var status="刚刚"
            if(digit<60){
                status="秒前";
                return digit+status;
            }
            else if(digit>=86400){
                status="天前";
                return Math.floor(digit/3600/24)+status;
            }
            else if(digit>=3600){
                status="小时前";
                return Math.floor(digit/3600)+status;
            }
            else if(digit>=60){
                status="分钟前";
                return Math.floor(digit/60)+status;
            }   
        }
            var p1=document.querySelector("p");
            p1.innerText= tm("2021-12-19 19:04:02");
    </script>
</body>
</html>